namespace ScrumSprintMonitor.ServerAdapter.TFS.TemplateAdaptors
{
	#region #using Directives

	using Infrastructure;

	using Isolation.TeamFoundationServer;

	#endregion

	public class FaroMsfAgileWorkItemAdapter : CoreWorkItemAdapter
	{
		public FaroMsfAgileWorkItemAdapter(IWorkItem workItem)
			: base(workItem)
		{
		}

		protected override void RetrieveType()
		{
			switch (WorkItem.Type)
			{
				case "Task":
					Type = WorkItemType.SprintBacklogItem;
					break;
				case "Bug":
					Type = WorkItemType.Defect;
					break;
				case "Product Backlog Item":
					Type = WorkItemType.UserStory;
					break;
				default:
					Type = WorkItemType.Unknown;
					break;
			}
		}

		protected override void RetrieveState()
		{
			switch (Type)
			{
				case WorkItemType.SprintBacklogItem:
				{
					switch (WorkItem.State)
					{
						case "Removed":
							State = WorkItemState.Deleted;
							break;
						case "In Progress":
							State = WorkItemState.InProgress;
							break;
						case "Done":
							State = WorkItemState.Done;
							break;
						default:
							State = WorkItemState.NotDone;
							break;
					}
					break;
				}
				default:
				{
					switch (WorkItem.State)
					{
						case "Removed":
							State = WorkItemState.Deleted;
							break;
						case "Approved":
						{
							var integrationBuild = (string) WorkItem["Microsoft.VSTS.Build.IntegrationBuild"];
							State = string.IsNullOrEmpty(integrationBuild) ? WorkItemState.InProgress : WorkItemState.Done;
							break;
						}
						default:
							State = WorkItemState.NotDone;
							break;
					}
					break;
				}
			}
		}

		protected override void RetrieveRemainingWork()
		{
			if (Type != WorkItemType.SprintBacklogItem)
			{
				RemainingWork = double.NaN;
				return;
			}

			object remainingWork = WorkItem["Microsoft.VSTS.Scheduling.RemainingWork"];
			RemainingWork = (remainingWork == null ? double.NaN : (double) remainingWork);
		}

		protected override void RetrieveDoneWork()
		{
			if (Type != WorkItemType.SprintBacklogItem)
			{
				DoneWork = double.NaN;
				return;
			}

			object completedWork = WorkItem["Microsoft.VSTS.Scheduling.CompletedWork"];
			DoneWork = (completedWork != null ? (double) completedWork : 0.0);
		}
	}
}
